#include <QDateTime>
#include "screenShot.h"
#include "logger.h"
#include "source/tools.h"
ScreenShot::ScreenShot(QObject *parent) : QObject(parent)
{

}

void ScreenShot::shootScreen(QObject *itemObj)
{
    grabItem = qobject_cast<QQuickItem*>(itemObj);
    grabResult = grabItem->grabToImage();

    QQuickItemGrabResult * grabResultData = grabResult.data();
    connect(grabResultData,SIGNAL(ready()),this,SLOT(saveimage()));
}

void ScreenShot::shootScreenWindow(QQuickWindow *rootWindow)
{
    QImage image = rootWindow->grabWindow();
    QString filePathName = "./master/screenshot/";
    ensureDirectoryExists(filePathName);
    filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss-zzz");
    filePathName += QString(".jpg");

    image.save(filePathName,"JPG");
}

void ScreenShot::saveimage()
{
    QString filePathName = "./master/screenshot/";
    ensureDirectoryExists(filePathName);
    filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss-zzz");
    filePathName += QString(".jpg");
    QImage img = grabResult->image();
    if(img.save(filePathName)){
        LogDebug <<"save sucessfully!";
    }
}
